import pymysql
from pymysql.cursors import DictCursor

CONFIG = {
    'host': '10.36.174.21',
    'port':3306,
    'user' : 'root',
    'password' : 'root',
    'db' : 'bank',
    'charset' : 'utf8',
    # 设置游标
    'cursorclass' : DictCursor
}

class DB():
    def __init__(self):
        self.conn = pymysql.Connect(**CONFIG)

    #  进入
    def __enter__(self):
        return self.conn.cursor()

    # 退出
    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type:
            # 有数据不为空，有异常
                self.conn.rollback()
        else:
            self.conn.commit()

    # 关闭
    def close(self):
        if self.conn:
            self.conn.close()
            self.conn = None

class BaseDao():
    def __init__(self):
        self.db = DB()

    def find_all(self,table,where=None,*whereArgs):
        sql = "select * from %s" % table
        if where:
            sql += where
        with self.db as c:
            c.execute(sql, whereArgs)
            result = list(c.fetchall())
            return result